<?php
	define('CONFIG_FILE',true);
	include '../config.php';
	
	define('DB_FILE',true);
	require_once '../Tool/DB/SqlDBManager.class.php';
	require_once '../Tool/Common/PrepareInput.php';

	session_start();
	if (empty($_SESSION["userType"])) {
		$url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
	}
	
	$CentreName = $_SESSION['centreName'];
	$username = $_SESSION["username"];
	$NRIC = $_POST['NRIC'];
	$activityCentre = $_POST['activityCentre'];
	$activityId = $_POST['activityId'];
	$date = date('Y-m-d',time());
	$performance = $_POST['performance'];
	if(!empty($_POST['remarks'])){
		$remarks = $_POST['remarks'];	
	}else{
		$remarks="NULL";
	}
	

	$sqlDBManager = new SqlDBManager();

	$sql="SELECT * From activityrecord where NRIC=? and ActivityId=? and date(activityrecord.Date)=CURDATE()";
	$parameters=array($NRIC,$activityId);

	$res=$sqlDBManager->queryRow($sql,$parameters);

	if(empty($res)){
		$sql = "INSERT INTO activityrecord (NRIC, CentreId, Date, ActivityId, AuditRecords, Performance,Remarks) VALUES (?,?,?,?,?,?,?)";
		$parameters=array($NRIC,$activityCentre,$date,$activityId,$username,$performance,$remarks);
		$res=$sqlDBManager->execute_dml($sql,$parameters);

		//close connection
		$sqlDBManager->close_connect();
		
		if ($res>0){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Add new activity attendance record failed due to database error.'));
		}
	}else{
		echo json_encode(array('msg'=>'The elderly has been assigned this activity already on today.'));
	}

?>